package ru.ifmo.network.skype;

import ru.ifmo.network.skype.network.data.UserAddress;

import java.io.Externalizable;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;

/**
 * @author avhaliullin
 */
public class UserData implements Externalizable {
    private String name;
    private final UserAddress userAddress;

    public UserData(UserAddress userAddress) {
        this.userAddress = userAddress;
    }

    public UserData(UserAddress userAddress, String name) {
        this(userAddress);
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public UserAddress getUserAddress() {
        return userAddress;
    }

    public void writeExternal(ObjectOutput out) throws IOException {
        out.writeObject(name);
        out.flush();
    }

    public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
        name = (String) in.readObject();
    }

    public boolean equals(Object o) {
        return (o instanceof UserData) && name.equals(((UserData) o).getName()) &&
                userAddress != null && userAddress.equals(((UserData) o).getUserAddress());
    }

    public int hashCode() {
        return (userAddress == null ? 0 : userAddress.hashCode()) + (name == null ? 0 : name.hashCode());
    }
}
